package cn.pping.basic.controller;

import cn.pping.basic.exception.BusinessExeption;
import cn.pping.basic.service.IVerifyCodeService;
import cn.pping.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;

    /**
     * @Title: getVerifyCode图形验证码
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/28 15:09
     * @Parameters: [key]
     * @Return cn.pping.basic.util.JsonResult
     */

    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key) {
        try {
            String codeBase = iVerifyCodeService.getVerifyCode(key);
            // 将数据封装给前端
            return JsonResult.me().setResultObj(codeBase);
        } catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("验证码获取失败，请稍后重试");
        }
    }

    /**
     * @Title: 注册手机验证码
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/8/1 17:35
     * @Parameters: [map]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PostMapping("/smsCode")
    public JsonResult sendSmsCode(@RequestBody Map<String, String> map) {
        try {
            iVerifyCodeService.sendSmsCode(map);
            // 将数据封装给前端
            return JsonResult.me();
        } catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("验证码获取失败，请稍后重试");
        }
    }

    /**
     * @Title: 绑定手机
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/8/1 17:35
     * @Parameters: [map]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String, String> map) {
        try {
            iVerifyCodeService.binderSmsCode(map);
            // 将数据封装给前端
            return JsonResult.me();
        } catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("验证码获取失败，请稍后重试");
        }
    }
}
